package com.gzhu.knowledgeAdmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzhu.knowledgeAdmin.common.utils.UserInfoUtils;
import com.gzhu.knowledgeAdmin.common.vo.KnowledgeFailDetailVo;
import com.gzhu.knowledgeAdmin.common.vo.NotPassVo;
import com.gzhu.knowledgeAdmin.common.vo.PageVo;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.gzhu.knowledgeAdmin.dao.ExamineDao;
import com.gzhu.knowledgeAdmin.entity.Examine;
import com.gzhu.knowledgeAdmin.service.ExamineService;
import org.springframework.transaction.annotation.Transactional;


@Service("examineService")
public class ExamineServiceImpl extends ServiceImpl<ExamineDao, Examine> implements ExamineService {

    @Override
    public PageVo queryPage(Map<String, Object> params) {
        // 获取当前页和每页大小
        long pageNo = Long.parseLong((String) params.get("page"));
        long pageSize = Long.parseLong((String) params.get("limit"));

        Page<Examine> page = new Page<>(pageNo,pageSize);
        QueryWrapper<Examine> wrapper = new QueryWrapper<>();
        // TODO 根据params里的查询参数添加条件查询

        baseMapper.selectPage(page,wrapper);

        return new PageVo(page);
    }

    /**
     * 记录审核员
     * @param userId 审核员id
     * @param zsIds
     */
    @Override
    public void setExamineUser(Integer userId, List<Integer> zsIds) {

        UpdateWrapper<Examine> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("examine_user",userId);
        updateWrapper.in("zs_id",zsIds);

        baseMapper.update(new Examine(),updateWrapper);
    }

    @Override
    public void setUnPass(NotPassVo notPassVo) {

        // 获取当前登录用户
        Integer userId = UserInfoUtils.getCurrentUser().getUserId();

        UpdateWrapper<Examine> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("examine_user",userId);
        updateWrapper.set("not_passed_reason",notPassVo.getNotPassedReason());
        updateWrapper.eq("zs_id", notPassVo.getZsId());

        baseMapper.update(new Examine(),updateWrapper);
    }

    /**
     * 获取知识审核不通过详情
     * @param id 知识id
     * @return
     */
    @Override
    public KnowledgeFailDetailVo getZsFailDetail(Integer id) {
       return baseMapper.getZsFailDetail(id);
    }

    @Override
    @Transactional
    public void reExamine(Integer id) {

        // 删除原来的审核记录
        baseMapper.deleteOld(id);

        // 添加一个新的
        Examine examine = new Examine();
        examine.setZsId(id);
        save(examine);
    }

}